import 'package:boss/models/commodity.dart';
import 'package:boss/models/package_group.dart';
import 'package:boss/models/store_menu.dart';
import 'package:flutter/material.dart';

class PackageTag {
  String tag;
  int value;

  PackageTag({this.tag, this.value});
}

List<PackageTag> packageTags = [
  PackageTag(tag: '无', value: 0),
  PackageTag(tag: '爆款标签', value: 1),
  PackageTag(tag: '人气标签', value: 2),
  PackageTag(tag: '新品标签', value: 3),
  PackageTag(tag: '热销标签', value: 4),
];

class PackageDetailProvider extends ChangeNotifier {
  Commodity package;

  List<StoreMenu> menus;

  int get sellDayIndex {
    if (package.commodityCellWeek == '0/1/2/3/4/5/6') {
      return 0;
    } else if (package.commodityCellWeek == '0/1/2/3/4') {
      return 1;
    } else if (package.commodityCellWeek == '5/6') {
      return 2;
    } else {
      return 3;
    }
  }

  List<int> get sellDays => package.commodityCellWeek.split('/').where((item) => item != '').map((item) => int.parse(item)).toList();

  String get sellStartTimeText {
    if (package.commodityCellTime == ''){
      return '';
    }

    return package.commodityCellTime.split('-')[0];
  }

  String get sellEndTimeText {
    if (package.commodityCellTime == ''){
      return '';
    }

    return package.commodityCellTime.split('-')[1];
  }

  int get sellTimeIndex {
    if (package.commodityCellTime == '00:00-23:59') {
      return 0;
    } else if (package.commodityCellTime == '10:00-14:00') {
      return 1;
    } else if (package.commodityCellTime == '17:00-23:00') {
      return 2;
    } else {
      return 3;
    }
  }

  PackageDetailProvider({this.package, this.menus});

  setPackageImage(String imageUrl) {
    package.images = imageUrl;
    notifyListeners();
  }

  setPackageName(String name) {
    package.title = name;
    notifyListeners();
  }

  setPackageMenu(StoreMenu menu) {
    package.menuId = menu.menuId;
    package.menuName = menu.menuName;
    notifyListeners();
  }

  setPackageMustCommodity(List<Commodity> commoditys) {
    var mustCommoditys = commoditys.map((item) => CommodityFormList.fromJson({
      'commodityId': item.id,
      'commodityName': item.title,
      'price': item.price,
      'skuId': item.sku?.skuId,
      'skuName': item.sku?.skuName,
      'unit': item.unit,
      'quantity': item.count,
    })).toList();

    package.mustCommodityList = mustCommoditys;
    notifyListeners();
  }

  removeMustCommodity(CommodityFormList commodity) {
    package.mustCommodityList.remove(commodity);
    notifyListeners();
  }

  sortMustCommodity(List<CommodityFormList> commoditys) {
    package.mustCommodityList = commoditys;
    notifyListeners();
  }

  setPackageGroup(List<PackageGroup> packageGroups) {
    package.packagesGroupList = packageGroups;
    notifyListeners();
  }

  removePackageGroup(PackageGroup packageGroup) {
    package.packagesGroupList.remove(packageGroup);
    notifyListeners();
  }

  setPackageGroupBarCode(String value) {
    package.commodityBarCode = value;
    notifyListeners();
  }

  setPackageDesc(String value) {
    package.description = value;
    notifyListeners();
  }

  setSellDay(List<int> days) {
    days.sort((a, b) => a- b);

    package.commodityCellWeek = days.join('/');
    notifyListeners();
  }

  setSellTime(String time) {
    package.commodityCellTime = time;
    notifyListeners();
  }

  setPackageTag(int index) {
    package.packagesTag = index;
    notifyListeners();
  }

  setPackagePrice(int price) {
    package.price = price;
    notifyListeners();
  }

  setH5(bool flag) {
    package.isH5Sell = flag ? 1 : 0;
    notifyListeners();
  }
}
